//
//  SCRefreshControl.m
//  Mutal
//
//  Created by TonyStart on 16/10/25.
//  Copyright © 2016年 com.wx. All rights reserved.
//

#import "SCRefreshControl.h"
#import "SCRefreshView.h"
#import "SCBottomRefreshView.h"
@interface SCRefreshControl()
@property(nonatomic,strong)SCRefreshView *refreshView;
@property(nonatomic,strong)UITableView *superTableView;
@property(nonatomic,strong)SCBottomRefreshView *bottomRefreshView;
@end
#define REFRESH_OFFSET 60
#define BOTTOM_REFRESH_OFFSET 40
@implementation SCRefreshControl
-(instancetype)init{
    if (self = [super init]) {
        self.isAllowDrop = YES;
        [self setupUI];
    }
    return self;
}

-(UIView *)bottomRefreshView{
    if (!_bottomRefreshView) {
        _bottomRefreshView = [[SCBottomRefreshView alloc] initWithFrame:CGRectMake(0,100, [UIScreen mainScreen].bounds.size.width, BOTTOM_REFRESH_OFFSET)];

        _bottomRefreshView.hidden = YES;

    }
    return _bottomRefreshView;
}
-(SCRefreshView *)refreshView{
    if (!_refreshView) {
        UINib *nib = [UINib nibWithNibName:@"SCRefresh" bundle:nil];
        _refreshView = [nib instantiateWithOwner:nil options:nil].firstObject;
        _refreshView.frame = CGRectMake(0, -60, [UIScreen mainScreen].bounds.size.width, 60);
    }
    return _refreshView;
}
-(void)setIsAllowDrop:(BOOL)isAllowDrop{
    _isAllowDrop = isAllowDrop;
    if (!_isAllowDrop) {
        [self.refreshView removeFromSuperview];
    }
}
-(void)setIsAllowPull:(BOOL)isAllowPull{
    _isAllowPull = isAllowPull;
    if (!_isAllowPull) {
        [self.bottomRefreshView removeFromSuperview];
    }
}
-(void)setupUI{
    _is_drop = YES;
    if (self.isAllowDrop) {
        [self addSubview:self.refreshView];
    }
    [self addSubview:self.bottomRefreshView];
}
//当添加视图到父视图的时候 添加监听
-(void)willMoveToSuperview:(UIView *)newSuperview{
    
    _superTableView = (UITableView*)newSuperview;
    
    [_superTableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    
}
//从父视图中移除监听
-(void)removeFromSuperview{
    [self.superview removeObserver:self forKeyPath:@"contentOffset"];
    [super removeFromSuperview];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{

   
    //判断是否到最上面了
    CGFloat offsetY = -(_superTableView.contentOffset.y + _superTableView.contentInset.top);

    //判断是上拉 还是下拉
    if (_superTableView.contentOffset.y<=0) {
        _is_drop = YES;
    }else{
        _is_drop = NO;
    }
    //判断是否到底底部
    CGFloat content_offset = _superTableView.contentOffset.y + _superTableView.frame.size.height;
    CGFloat bottomOffset = _superTableView.contentSize.height + BOTTOM_REFRESH_OFFSET;
    //如果是拖拽情况下
    if (_superTableView.isDragging) {
        //下拉
        if (_is_drop) {
            if (offsetY>=REFRESH_OFFSET && self.refreshState == Normal){
                self.refreshState = Pulling;
                [self.refreshView changeContentWithRefreshState:self.refreshState];
            }else if (offsetY<REFRESH_OFFSET && self.refreshState == Pulling){
                self.refreshState = Normal;
                [self.refreshView changeContentWithRefreshState:self.refreshState];
            }
        }else{//上拉
            self.bottomRefreshView.frame = CGRectMake(0, _superTableView.contentSize.height, [UIScreen mainScreen].bounds.size.width, BOTTOM_REFRESH_OFFSET);
            
            if (content_offset >= bottomOffset){
                self.bottomRefreshView.hidden = NO;
                self.refreshState = Pulling;
                
                [self.bottomRefreshView changeContentWithRefreshState:self.refreshState];
            }else{
                self.refreshState = Normal;
                [self.bottomRefreshView changeContentWithRefreshState:self.refreshState];
            }
            
        }
    }else{
        //进入这里表示拖拽结束 如果是超过临界点就开始刷新
       
        if (self.refreshState == Pulling) {
            //发送事件消息
            [self sendActionsForControlEvents:UIControlEventValueChanged];
            
        }
    }

}

///开始刷新
-(void)beginRefreshing:(void (^)(BOOL))finish{

    if (self.refreshState == WillRefresh){
        return;
    }
    //已经进入刷新数据状态
    self.refreshState = WillRefresh;
    if (_is_drop) {
        //保持刷新状态
        [self.refreshView changeContentWithRefreshState:self.refreshState];
        UIEdgeInsets inset = self.superTableView.contentInset;
        inset.top += REFRESH_OFFSET;
        self.superTableView.contentInset = inset;
    }else{
        //保持刷新状态
        [self.bottomRefreshView changeContentWithRefreshState:self.refreshState];
        UIEdgeInsets inset = self.superTableView.contentInset;
        inset.bottom += BOTTOM_REFRESH_OFFSET;
        self.superTableView.contentInset = inset;
    }
    finish(_is_drop);
    
}
///结束刷新
-(void) endRefreshing{

    
    if (self.refreshState != WillRefresh) {
        return;
    }
    if (_is_drop) {
        //保持刷新状态
        [UIView animateWithDuration:0.25 animations:^{
            UIEdgeInsets inset = self.superTableView.contentInset;
            inset.top -= REFRESH_OFFSET;
            self.superTableView.contentInset = inset;
            
        } completion:^(BOOL finished) {
            self.refreshState = Normal;
            [self.refreshView changeContentWithRefreshState:self.refreshState];
        }];
    }else{
        //保持刷新状态
        [UIView animateWithDuration:0.25 animations:^{
            UIEdgeInsets inset = self.superTableView.contentInset;
            inset.bottom -= BOTTOM_REFRESH_OFFSET;
            self.superTableView.contentInset = inset;
            
        } completion:^(BOOL finished) {
            self.bottomRefreshView.hidden = YES;
            
            self.refreshState = Normal;
            [self.bottomRefreshView changeContentWithRefreshState:self.refreshState];
        }];
    }
    
}


@end
